package com.shire.shire_interface.service.timeline;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.dao.timeline.WxFriendTimelineDao;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.timeline.WxFriendTimeline;
import com.shire.shire_interface.model.timeline.WxFriendTimelineExample;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.account.WxAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxFriendTimelineService {
    @Resource
    private WxFriendTimelineDao wxFriendTimelineDao;
    @Resource
    private WxAccountService accountService;

    public List<WxFriendTimeline> selectByExample(WxFriendTimelineExample example){
        return wxFriendTimelineDao.selectByExampleWithBLOBs(example);
    }

    public int updateFriendTimeline(WxFriendTimeline friendTimeline){
        return wxFriendTimelineDao.updateByPrimaryKeySelective(friendTimeline);
    }

    public WxFriendTimeline selectByTid(String wxAccount, String tid){
        WxFriendTimelineExample example = new WxFriendTimelineExample();
        example.createCriteria().andWxaccountEqualTo(wxAccount).andTimelineidEqualTo(tid);
        List<WxFriendTimeline> timelineList = wxFriendTimelineDao.selectByExampleWithBLOBs(example);
        if (timelineList.size() > 0)
            return timelineList.get(0);

        return null;
    }

    public List<WxFriendTimeline> recieveTimeline(WxCommand command, JSONObject jsonObject){
        if (jsonObject == null){
            return new ArrayList<>();
        }
        JSONArray  contactList = jsonObject.getJSONArray("contacts");
        JSONArray timelineList = jsonObject.getJSONArray("timelines");

        List<WxFriendTimeline> returnList = new ArrayList<>();
        WxAccount account = accountService.getWxAccountById(command.getAccountid());
        if (timelineList.size() < 1) return returnList;

        Map<String, JSONObject> contactMap = new HashMap<>();
        for (Object obj : contactList) {
            JSONObject cobj = (JSONObject)obj;
            contactMap.put(cobj.getString("wxId"),cobj);
        }

        for (Object o : timelineList) {
            JSONObject timelineObj = (JSONObject)o;

            String sender = timelineObj.getString("senderWxId");
            timelineObj.put("sender", contactMap.get(sender)); //完善发送者信息
            List<JSONObject> likedUsers = new ArrayList<>();
            JSONArray likeWxIds = timelineObj.getJSONArray("likeWxIds");
            for (Object likeWxId : likeWxIds) {
                String wxid = (String) likeWxId;
                likedUsers.add(JSONObject.parseObject(contactMap.get(wxid).toJSONString()));
            }
            timelineObj.put("likeUsers", likedUsers); //完善点赞用户信息
            JSONArray comments = timelineObj.getJSONArray("comments");
            if (comments != null) {
                for (Object comment : comments) {
                    JSONObject commentJSON = (JSONObject) comment;
                    commentJSON.put("sender", JSONObject.parseObject(contactMap.get(commentJSON.getString("senderWxId")).toJSONString()));
                }
            }

            //保存
            String tid = timelineObj.getString("tid");
            WxFriendTimelineExample friendTimelineExample = new WxFriendTimelineExample();
            friendTimelineExample.createCriteria().andTimelineidEqualTo(tid);

            List<WxFriendTimeline> wxFriendTimelineList = wxFriendTimelineDao.selectByExampleWithBLOBs(friendTimelineExample);
            WxFriendTimeline timeline;

            if (wxFriendTimelineList.size()<1){
                timeline = new WxFriendTimeline();
            }else {
                timeline = wxFriendTimelineList.get(0);
            }

            timeline.setWxaccount(account.getWxid());
            timeline.setTimelinecontent(timelineObj.toJSONString());
            timeline.setCsid(command.getCsid());
            timeline.setFriendwxid(sender);
            timeline.setTimelineid(tid);
            if (timeline.getId() == null)
                wxFriendTimelineDao.insertSelective(timeline);
            else
                wxFriendTimelineDao.updateByPrimaryKeySelective(timeline);
            returnList.add(timeline);
        }
        return returnList;
    }
}
